package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.pojo.AddressBook;
import com.itheima.pojo.Orders;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*订单*/
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * <p>Description: 下单</p>
     * 
     * @author liuyiming
     * @date 2022-10-15 18:26
     * @version 1.0.0
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order){
        log.info("用户下单，订单数据：{}",order);
        ordersService.submit(order);
        return R.success("下单成功");
    }

    /**
     * <p>Description: 分页查询所有订单信息</p>
     *
     * @author liuyiming
     * @date 2022-10-16 18:08
     * @version 1.0.0
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,Long number,String beginTime,String endTime){
        log.info("分页查询所有订单信息");
        Page<Orders> pageInfo = ordersService.pageInfo(page, pageSize, number, beginTime, endTime);
        return R.success(pageInfo);
    }
    
    /**
     * <p>Description: 修改状态</p>
     * 
     * @author liuyiming
     * @date 2022-10-16 18:59
     * @version 1.0.0
     */
    @PutMapping
    public R<String> updateOrderStatus(@RequestBody Orders order){
        log.info("修改订单状态");
        LambdaUpdateWrapper<Orders> luw = new LambdaUpdateWrapper<>();
        luw.eq(Orders::getStatus,order.getStatus());
        ordersService.updateById(order);
        return R.success("修改订单状态成功");
    }

    /**
     * <p>Description: 用户分页查询自己订单</p>
     *
     * @author liuyiming
     * @date 2022-10-16 19:12
     * @version 1.0.0
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){
        log.info("用户分页查询订单信息");
        Page pageInfo = ordersService.pageInfo(page, pageSize);
        return R.success(pageInfo);
    }

    /**
     * <p>Description: 再来一单</p>
     *
     * @author liuyiming
     * @date 2022-10-17 14:23
     * @version 1.0.0
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders) {
        ordersService.again(orders);
        return R.success("下单成功");
    }

}
